package com.kl.modular.sysman.system.controller;

import com.alibaba.fastjson.JSON;
import com.kl.common.pager.PageParameter;
import com.kl.common.pager.Pager;
import com.kl.common.util.UUIDUtil;
import com.kl.modular.common.controller.BaseController;
import com.kl.modular.common.response.ResponseBase;
import com.kl.modular.system.dao.bo.SystemRoleBo;
import com.kl.modular.system.dao.model.SystemRole;
import com.kl.modular.system.service.api.ISystemRoleService;
import com.kl.modular.system.service.constant.SystemConStant;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 角色控制器
 * @author XRP_ZJB
 * @date 2016-08-05 22:11
 * @(C) XIGNSOFT Copyright 
 */
@Controller
public class SystemRoleController extends BaseController {
	
	private static final Logger logger = LoggerFactory.getLogger(SystemRoleController.class);
	
	@Autowired
	private ISystemRoleService systemRoleService;
	
	/** 角色 view */
	private static String pathSystemRoleRoot="/sysman/system/role/";
	
	
	/**
	 * 角色列表页面
	 */
	@RequestMapping(value = "/sysman/systemRole/systemRoleList", method = RequestMethod.GET)
	@RequiresPermissions("sysman:SYSTEM_ROLE_MAN:viewlist")
	public String queryList(String roleModuleType, HttpServletRequest request) {
		logger.info("角色列表页面");
		request.setAttribute("roleModuleType", roleModuleType);
		return pathSystemRoleRoot + "systemRoleList.jsp";
	}

	/**
	 * 角色列表数据
	 */
	@RequestMapping(value = "/sysman/systemRole/systemRoleAjaxList", method = RequestMethod.POST)
	@RequiresPermissions("sysman:SYSTEM_ROLE_MAN:viewlist")
	public String queryAjaxList(SystemRoleBo systemRoleBo, PageParameter pageParameter , HttpServletRequest request) {
		logger.info("systemRole列表数据>>data:" + JSON.toJSONString(systemRoleBo));
		if (systemRoleBo == null) {
			systemRoleBo = new SystemRoleBo();
		}
		if(pageParameter == null){
			pageParameter = new PageParameter();
		}
		Pager<SystemRole> page = systemRoleService.getListByBoPage(systemRoleBo,pageParameter);
		request.setAttribute("pager", page);
		return pathSystemRoleRoot + "systemRoleAjaxList.jsp";
	}
	
	
	/**
	 * 角色添加页面
	 * @return
	 */
	@RequestMapping(value="/sysman/systemRole/toSystemRoleAdd" , method = RequestMethod.GET)
	@RequiresPermissions("sysman:SYSTEM_ROLE_MAN:add")
	public String toAddSystemRole(){
		logger.info("角色添加页面");
		return pathSystemRoleRoot + "addSystemRole.jsp";
	}
	
	/**
	 * 添加角色
	 */
	@RequestMapping(value="/sysman/systemRole/addSystemRole" , method = RequestMethod.POST)
	@ResponseBody
	@RequiresPermissions("sysman:SYSTEM_ROLE_MAN:add")
	public String addSystemRole(SystemRole systemRole,String authResourceStr,HttpServletResponse response) throws Exception{
		logger.info("添加角色>>data:" + JSON.toJSONString(systemRole));

		ResponseBase base = new ResponseBase();

		// 数据验证
		if(StringUtils.isEmpty(authResourceStr)){
			logger.error("添加角色数据验证>>选择的权限信息为空");
			base.setMsg("请选择角色所拥有的权限");
			return JSON.toJSONString(base);
		}
		if (systemRole == null) {
			logger.error("添加角色数据验证>>systemRole is null ");
			base.setMsg(ResponseBase.RESPOSNE_NOT_VALID_ERROR);
			return JSON.toJSONString(base);
		}
		
		if(StringUtils.isEmpty(systemRole.getRoleName())){
			logger.error("添加角色数据验证>>角色名称 is null ");
			base.setMsg("角色名称不能为空");
			return JSON.toJSONString(base);
		}
		if(StringUtils.isEmpty(systemRole.getRoleShrioCode())){
			logger.error("添加角色数据验证>>角色标识code is null ");
			base.setMsg("角色标识code不能为空");
			return JSON.toJSONString(base);
		}
		
		systemRole.setRoleCode(UUIDUtil.getUniquekey());
		systemRole.setStatus(SystemConStant.SYSTEM_ADMIN_STATUS_NORMAL);
		systemRoleService.addSystemRole(systemRole,authResourceStr);

		base.setStatus(ResponseBase.RESPONSE_STATUS_SUCCESS);
		String jsonstr = JSON.toJSONString(base);
		logger.info("添加角色>>返回>>data:" + jsonstr);
		return jsonstr;
	}

	
	/**
	 * 角色更新页面
	 * @return
	 */
	@RequestMapping(value = "/sysman/systemRole/toChangeView" , method = RequestMethod.GET)
	@RequiresPermissions("sysman:SYSTEM_ROLE_MAN:edit")
	public String toChangeSystemRole(String id,HttpServletRequest request) throws Exception{
		logger.info("角色修改页面>>data:" + id);
		SystemRole systemRole = systemRoleService.getSystemRoleByCode(id);
		request.setAttribute("item", systemRole);
		return pathSystemRoleRoot + "systemRoleUpChange.jsp";
	}

	
	/**	
	 * 修改
	 */
	@RequestMapping(value = "/sysman/systemRole/upChange" , method = RequestMethod.POST)
	@ResponseBody
	@RequiresPermissions("sysman:SYSTEM_ROLE_MAN:edit")
	public String upChangeSystemRole(SystemRole systemRole,String authResourceStr,HttpServletResponse response) throws Exception{
		logger.info("更新角色信息>>data:" + JSON.toJSONString(systemRole));

		ResponseBase base = new ResponseBase();

		// 数据验证
		if (systemRole == null) {
			logger.error("更新角色数据验证>>systemRole is null");
			base.setMsg(ResponseBase.RESPOSNE_NOT_VALID_ERROR);
			return JSON.toJSONString(base);
		}
		if(StringUtils.isEmpty(systemRole.getRoleCode())){
			logger.error("更新角色数据验证>>角色code is null");
			base.setMsg("角色code不能为空");
			return JSON.toJSONString(base);
		}
		if(StringUtils.isEmpty(systemRole.getRoleName())){
			logger.error("更新角色数据验证>>角色名称 is null");
			base.setMsg("角色名称不能为空");
			return JSON.toJSONString(base);
		}
		if(StringUtils.isEmpty(systemRole.getRoleShrioCode())){
			logger.error("更新角色数据验证>>角色标识code sysAdmin超级管理员 is null");
			base.setMsg("角色标识code sysAdmin超级管理员不能为空");
			return JSON.toJSONString(base);
		}
		systemRoleService.updateByCode(systemRole,authResourceStr);

		base.setStatus(ResponseBase.RESPONSE_STATUS_SUCCESS);
		String jsonstr = JSON.toJSONString(base);
		logger.info("编辑角色信息>>返回>>data:" + jsonstr);
		return jsonstr;
	}

	/**
	 * 批量删除角色
	 */
	@RequestMapping(value = "/sysman/systemRole/delSystemRole" , method = RequestMethod.POST)
	@ResponseBody
	@RequiresPermissions("sysman:SYSTEM_ROLE_MAN:delete")
	public String delSystemRole(String id,HttpServletResponse response) throws Exception{
		logger.info("批量删除角色>>data:" + JSON.toJSONString(id));
		ResponseBase base = new ResponseBase();

		// 数据验证
		if (StringUtils.isEmpty(id)) {
			logger.info("数据验证>>删除 id is null");
			base.setMsg("请求数据有误");
			return JSON.toJSONString(base);
		}

		String[] codestrs = id.split(",");

		if (codestrs == null || codestrs.length == 0) {
			logger.info("数据验证>>删除角色资源>>codestrs is null");
			base.setMsg("参数错误，请选择要删除的角色");
			return JSON.toJSONString(base);
		}

		systemRoleService.deleteByCode(codestrs);
		base.setStatus(ResponseBase.RESPONSE_STATUS_SUCCESS);

		String jsonstr = JSON.toJSONString(base);
		logger.info("删除角色资源>>返回view>>data:" + jsonstr);
		return jsonstr;
	}
}
